package com.hzf.material.admin.controller.manager;


import com.hzf.material.dao.model.MaterialGoodsTypeMiddle;
import com.hzf.material.dao.model.MaterialGoodsTypeMiddleExample;
import com.hzf.material.dao.model.MaterialGoodsTypeSmall;
import com.hzf.material.dao.model.MaterialGoodsTypeSmallExample;
import com.hzf.material.rpc.api.MaterialGoodsTypeMiddleService;
import com.hzf.material.rpc.api.MaterialGoodsTypeSmallService;
import com.hzf.ucenter.rpc.common.constant.UcenterResult;
import com.hzf.ucenter.rpc.common.constant.UcenterResultConstant;
import com.hzf.upms.common.constant.UpmsResult;
import com.hzf.upms.common.constant.UpmsResultConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe: TODO
 * creat_user: 白开水
 * creat_time: 2017/11/20   18:44
 **/
@Controller
@Api(value = "材料商商品类型管理", description = "材料商商品管理")
@RequestMapping("/manage/goodsTypeSmall")
public class MaterialGoodsTypeSmallController {
    private static Logger _log = LoggerFactory.getLogger(MaterialGoodsTypeSmallController.class);
    @Autowired
    private MaterialGoodsTypeSmallService materialGoodsTypeSmallService;

    @ApiOperation(value = "基础小类首页")
    @RequiresPermissions("	material:goodstypesmall:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/materialGoodsTypeSmall/index.jsp";
    }
    @ApiOperation(value="基础小类列表")
    @RequiresPermissions("	material:goodstypesmall:read")
    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order){
        MaterialGoodsTypeSmallExample materialGoodsTypeSmallExample = new MaterialGoodsTypeSmallExample();
        if(!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)){
            materialGoodsTypeSmallExample.setOrderByClause(sort+" "+order);
        }
        List<MaterialGoodsTypeSmall> rows =materialGoodsTypeSmallService.selectByExampleForOffsetPage(materialGoodsTypeSmallExample,offset,limit);
        long total = materialGoodsTypeSmallService.countByExample(materialGoodsTypeSmallExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }
    @ApiOperation(value = "新增小类")
    @RequiresPermissions("	material:goodstypesmall:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {
        return "/manage/materialGoodsTypeSmall/create.jsp";
    }
    
    @ApiOperation(value = "新增小类")
    @RequiresPermissions("	material:goodstypesmall:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(MaterialGoodsTypeSmall materialGoodsTypeSmall) {
        int count = materialGoodsTypeSmallService.insertSelective(materialGoodsTypeSmall);
        return new UcenterResult(UcenterResultConstant.SUCCESS, count);
    }
    
    @ApiOperation(value = "删除小类")
    @RequiresPermissions("	material:goodstypesmall:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = materialGoodsTypeSmallService.deleteByPrimaryKeys(ids);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改小类")
    @RequiresPermissions("	material:goodstypesmall:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        MaterialGoodsTypeSmall materialGoodsTypeSmall = materialGoodsTypeSmallService.selectByPrimaryKey(id);
        modelMap.put("materialGoodsTypeSmall", materialGoodsTypeSmall);
        return "/manage/materialGoodsTypeSmall/update.jsp";
    }

    @ApiOperation(value = "修改小类")
    @RequiresPermissions("	material:goodstypesmall:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, MaterialGoodsTypeSmall materialGoodsTypeSmall) {
        materialGoodsTypeSmall.setSmallId(id);
        int count = materialGoodsTypeSmallService.updateByPrimaryKeySelective(materialGoodsTypeSmall);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

}
